package com.qzyh.esb.egsa.controller;

import com.qzyh.esb.egsa.utils.ResultObject;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(value = "自定义错误Controller", tags = { "自定义错误页面处理接口" })
@RestController
public class MyErrorController implements ErrorController {
    //通过LogFactory获取Logger类的实例
    private static final Logger logger = LoggerFactory.getLogger(MyErrorController.class);

    @RequestMapping("/myError")
    @ApiOperation(value = "自定义错误页面处理")
    @ApiResponses(value = { @ApiResponse(code = 1, message = "系统繁忙，请稍后重试") })
    public ResultObject myError(@ApiParam("http请求参数") HttpServletRequest request, @ApiParam("http响应参数") HttpServletResponse response) {
        //获取触发该error的uri
        String errorRequestUri = (String) request.getAttribute("javax.servlet.error.request_uri");
        String method = request.getMethod();
        logger.error("异常请求链接为："+errorRequestUri+",请求方式："+method);

        //修改http状态码为200
        response.setStatus(HttpStatus.OK.value());
        return ResultObject.error();
    }
}